﻿using JESAI.EventBus.Attributes;
using JESAI.EventBus.Contexts;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;

namespace JESAI.EventBus.Handles
{
    public class EventSubscribeHandle
    {

        internal EventSubscribeHandle(string eventId)
        {
            EventId = eventId;
        }

        internal Func<EventExecutingContext, Task> Handle { get; set; }

        public EventSubscribeAttribute Attribute { get; set; }

        public MethodInfo HandleMethod { get; set; }

        /// <summary>
        /// 是否启用执行完成触发 GC 回收
        /// </summary>
        public bool IsEnabledGCCollect { get; set; }

        /// <summary>
        /// 正则表达式
        /// </summary>
        internal Regex Pattern { get; set; }

        internal string EventId { get; set; }

        public int Order { get; set; } = 0;

        internal bool CanRun(string eventId)
        {
            return EventId == eventId || (Pattern?.IsMatch(eventId) ?? false);
        }
    }
}
